package com.xxgc.demo.controller.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @Author:SJY
 * @Date :2023/9/7 - 09 - 07 - 19:44
 * 全局统一返回对象
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
@JsonInclude(JsonInclude.Include.NON_NULL)//如果对象转json时字段为空就直接移除
public class Result<T> {
    @Schema(description = "系统内部状态码")
    private int code;

    @Schema(description = "系统提示信息")
    private String msg;

    @Schema(description = "总条数")
    private Long total;

    @Schema(description = "返回数据")
    private T data;

    @Schema(description = "异常信息")
    private String errorMsg;

    /**-----------------------------部分内容改变-----------------------------------**/
    public Result code(int code){
        this.code = code;
        return this;
    }

    public Result msg(String msg){
        this.msg = msg;
        return this;
    }

    public Result errorMsg(String errorMsg){
        this.errorMsg = errorMsg;
        return this;
    }

    public Result data(T data){
        this.data = data;
        return this;
    }

    public Result total(Long total){
        this.total = total;
        return this;
    }

    /**------------------------自定义构造函数-----------------------------------**/
    public Result(int code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static <T> Result<T> ok(T data){
        return new Result(200,"请求成功",data);
    }
    //成功的无参数版本
    public static <T> Result<T> ok(){ return new Result(200,"请求成功",null); }
    //失败请情况
    public static <T> Result<T> error(String msg){ return new Result(500,msg,null); }
    //异常使用的
    public static <T> Result<T> error(int code,T data){
        return new Result(code,"请求异常",data);
    }
    //不填参数的error
    public static <T> Result<T> error(){ return new Result(500,"请求异常",null); }
}
